package com.hw.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.pojo.HwCategory;
import com.hw.pojo.HwEntry;
import com.hw.service.DocumentService;
import com.hw.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: DocumentController
 * @Description: 承上启下
 * @Author: vie0134
 * @Date: 2020-12-25 15:09
 * @Version: 1.0
 **/
@Controller
@RequestMapping("document")
public class DocumentController {
    @Autowired
    private DocumentService documentService;

    @RequestMapping("list")
    public String documentList(HttpSession session){
        List<HwCategory> listName = documentService.findListName();
        System.out.println(listName);
        session.setAttribute("listName",listName);
        return "/html/list.html";
//        return "jsp/insertOrUpdate.jsp";
    }

    @RequestMapping("select")
    @ResponseBody
    public R select(@RequestParam(defaultValue = "1") Integer page,
                    @RequestParam(defaultValue = "5") Integer limit){
        PageHelper.startPage(page,limit);
        R r = new R();
        r.setCode(0);
        r.setMsg("");
        List<HwEntry> listEntry = documentService.findListEntry();
        PageInfo pageInfo = new PageInfo(listEntry);
        r.setCount((int) pageInfo.getTotal());
        r.setData(pageInfo.getList());
        return r;
    }

    @RequestMapping("toInsert")
    public String toInsert(Model model){
        List<HwCategory> listName = documentService.findListName();
        System.out.println(listName);
        model.addAttribute("listName",listName);
        return "/jsp/insertOrUpdate.jsp";
    }

    @RequestMapping("insert")
    public String insert(HwEntry hwEntry){
        System.out.println(hwEntry);
        if(documentService.insertEntry(hwEntry )){
            return "redirect:/document/list";
        }else {
            return "/jsp/insertOrUpdate.jsp";
        }
    }

    @RequestMapping("delete")
    public String delete(Integer id){
        if(documentService.deleteEntry(id)){
            //return "success";
            return "redirect:/document/list";
        }
        //return "falied";
        return "redirect:/document/list";
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Integer id, Model model){
        HwEntry entry = documentService.findEntryById(id);
        model.addAttribute("entry",entry);
        List<HwCategory> listName = documentService.findListName();
        model.addAttribute("listName",listName);
        return "/jsp/insertOrUpdate.jsp";
    }

    @RequestMapping("update")
    public String update(HwEntry entry){
        System.out.println(entry);
        if(documentService.updateEntry(entry)){
            return "redirect:/document/list";
        }else {
            return "/jsp/insertOrUpdate.jsp";
        }
    }

}
